import axios from 'axios'

export function getUrlStr(params) {
    let result = "?";
    params &&
      Object.keys(params).forEach(
        (item) => (result += `${item}=${params[item]}&`)
      );
    return result.slice(0, -1);
}
export function downImgFile(url, name) {
  const aLink = document.createElement('a')
  aLink.download = name 
  aLink.href = url 
  aLink.dispatchEvent(new MouseEvent('click', {}))
}


export function downloadFn(_fileName){
  axios({
    url:`${process.env.VUE_APP_BASE_API}/common/download?fileName=${_fileName}`,
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    },
    method: 'get',
    responseType:'blob',
  }).then(_res=>{
      const content = _res.data
      // const blob = new Blob([content], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'})

      // 自动下载blob文件流xls文件----------------------------------------
      const blob = new Blob([content], {
          type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8",
      });//防止下载的文件乱码
      const url = window.URL.createObjectURL(blob);
      const link = document.createElement("a");
      link.style.display = "none";
      link.href = url;
      link.setAttribute(
          "download",
          _fileName//后缀名一定要用xlsx，否则会乱码
      ); //设置下载文件名
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link); // 下载完成移除元素
      window.URL.revokeObjectURL(url); // 释放掉blob对象
  })


}